package com.swak.frame.excel;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

public class ExcelUtils {


    public final static int FIRST_ROW_NUMBER = 4;

    public final static String EXCEL_SUFFEX_XLS = ".xls";

    public final static String EXCEL_SUFFEX_XLSX = ".xlsx";

    public static final String FRISTDATAFLAG = "id";

    public static final int BUFFSIZE = 1024;

    public static String getSelectValbyKey(String value, List<Map<String, String>> selectVals) {
        Map<String, String> valMap = null;
        if (selectVals == null || selectVals.size() == 0 || value == null || value == "") {
            return value;
        }
        for (Map<String, String> keyMap : selectVals) {
            String kv = keyMap.get("value");
            if (value.equals(kv)) {
                valMap = keyMap;
                break;
            }
        }
        String tempVal = value;
        if (valMap != null) {
            tempVal = valMap.get("value") + "=" + valMap.get("display");
        }
        return tempVal;
    }

    public static String matcherLastValue(String src, String pattern) {
        if (src == "" || src == null) {
            return src;
        }
        if (pattern == null) {
            pattern = "\\[([^\\[\\]]*)\\]";
        }
        Pattern patter = Pattern.compile(pattern);
        Matcher matcher = patter.matcher(src);
        String lastStr = "";
        while (matcher.find()) {
            lastStr = matcher.group(1);
        }

        if (StringUtils.isEmpty(lastStr)) {
            return src.trim();
        }

        return lastStr.trim();
    }

    public static String getExcelColumnLabel(int num) {
        String temp = "";
        // make sure how many letters are there
        double i = Math.floor(Math.log(25.0 * (num) / 26.0 + 1) / Math.log(26)) + 1;
        if (i > 1) {
            double sub = num - 26 * (Math.pow(26, i - 1) - 1) / 25;
            for (double j = i; j > 0; j--) {
                temp = temp + (char) (sub / Math.pow(26, j - 1) + 65);
                sub = sub % Math.pow(26, j - 1);
            }
        } else {
            temp = temp + (char) (num + 65);
        }
        return temp;
    }


    public static String getExcelFile(String tempPath) {
        File baseFile = new File(tempPath);
        if (baseFile.isDirectory()) {
            File[] files = baseFile.listFiles();
            for (int i = 0; i < files.length; i++) {
                File f = files[i];
                if (f.isFile()) {
                    String fileName = f.getName();
                    if (fileName.endsWith(EXCEL_SUFFEX_XLS)
                        || fileName.endsWith(EXCEL_SUFFEX_XLSX)) {
                        return fileName;
                    }
                }
            }
        }
        return null;
    }

    public static int[] getBlankNameColNumber(Map<String, String> header, String[] notBlankCol) {
        if (ArrayUtils.isEmpty(notBlankCol)) {
            return new int[0];
        }
        int[] cols = new int[notBlankCol.length];
        for (int col = 0; col < notBlankCol.length; col++) {
            for (int i = 0; i < header.size(); i++) {
                String name = header.get(String.valueOf(i));
                if (name != null && name.equals(notBlankCol[col])) {
                    cols[col] = i;
                }
            }
        }
        return cols;
    }

}
